package com.web.controller;

import com.annotation.RequestLimit;
import com.core.dto.FileUploadDTO;
import com.core.model.lxf.ShoppingCart;
import com.core.shared.ImageUtils;
import com.service.LxfService;
import com.web.bean.TestQualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created with IntelliJ IDEA.
 * User: xf
 * Date: 2016/4/12
 * Time: 22:17
 * To change this template use Editor | File and Code Templates.
 */
@Controller
@RequestMapping("/aop")
public class AopController {

    @Autowired
    private LxfService lxfService;
    @Autowired
    private ShoppingCart shoppingCart;
    @Autowired
    @Qualifier("qua1")
    private TestQualifier testQualifier;

    @RequestMapping(value = "learn")
    public void learnAop(HttpServletResponse response, HttpServletRequest request) {
        lxfService.aopLearn(response, request);
    }

    @RequestMapping(value = "shop")
    public void shoppingCart() {
        System.out.println("before: " + shoppingCart.getName() + "---" + shoppingCart.getPrice() + "hashcode= " + shoppingCart.hashCode());
        shoppingCart.setName("外套");
        shoppingCart.setPrice("599");
        System.out.println("update: " + shoppingCart.getName() + "---" + shoppingCart.getPrice() + "hashcode= " + shoppingCart.hashCode());
    }

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@Valid FileUploadDTO fileUploadDTO, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "front/file";
        }
        ImageUtils.upload("d:\\", fileUploadDTO.getFile());
        return "success";
    }

    @RequestMapping(value = "limit")
    @RequestLimit(count = 2)
    public String requestLimit(HttpServletRequest request) {
        return "test";
    }

}
